/* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html
$Id: component.js,v 1.2 2006/11/15 22:46:27 gmurray71 Exp $
*/

jmaki.DContainer = function(_widget){
    var _this = this;
    var _container = document.getElementById(_widget.uuid);
    var _topic = "/jmaki/dcontainer";

    this.url = null;
    //_container.style.overflow = "hide";
    
    if (typeof _widget.args != 'undefined'){
        if (typeof _widget.args.height != 'undefined'){
            var startHeight = _widget.args.height;
        }
        if (typeof _widget.args.width != 'undefined'){
            var startWidth = _widget.args.width;
        }
        
        if (typeof _widget.args.overflow != 'undefined'){
             _container.style.overflow = _widget.args.overflow;
        }
    }
  
    var autoSizeH = false;
    var autoSizeW = false;
  
    // default sizes are all based on the width of the container   
    var VIEWPORT_WIDTH;

    if (typeof startWidth != 'undefined') {
        VIEWPORT_WIDTH = Number(startWidth);
        _container.style.width = VIEWPORT_WIDTH + "px";
    } else {
        VIEWPORT_WIDTH = _container.clientWidth;
        autoSizeW = true;
    }

    var VIEWPORT_HEIGHT;

    if (typeof startHeight != 'undefined') {
        VIEWPORT_HEIGHT = Number(startHeight);
        _container.style.height = VIEWPORT_HEIGHT + "px";
    } else {
        VIEWPORT_HEIGHT = _container.clientHeight;
        _container.style.height = VIEWPORT_HEIGHT + "px";
        autoSizeH = true;
    }
    
    function getPosition(_e) {
        var pX = 0;
        var pY = 0;

        while (_e.offsetParent) {
                pY += _e.offsetTop;
                pX += _e.offsetLeft;
                _e = _e.offsetParent;
        }
        return {x: pX, y: pY};
    }
    
    this.resize = function() {
        var pos = getPosition(_container);
        if (autoSizeH) {
            if (_container.parentNode.nodeName == "BODY") {
                if (window.innerHeight){
                    VIEWPORT_HEIGHT = window.innerHeight - pos.y -10;
                } else {
                    VIEWPORT_HEIGHT = document.documentElement.clientHeight - pos.y - 40;
                }
                _container.style.height = VIEWPORT_HEIGHT + "px";
            } else {
                var _tNode = _container.parentNode;
                while(_tNode != null &&
                _tNode.clientHeight == 0) {
                    _tNode = _tNode.parentNode;
                }
                if (_tNode == null) {
                    VIEWPORT_HEIGHT = 400;
                } else {
                    VIEWPORT_HEIGHT = _tNode.clientHeight -1;
                }
                _container.style.height = VIEWPORT_HEIGHT + "px";
            }
        }
        
        if (autoSizeW) {
            var _tNode = _container.parentNode;
            while(_tNode != null &&
            _tNode.clientWidth == 0) {
                _tNode = _tNode.parentNode;
            }
            if (_tNode == null) {
                VIEWPORT_WIDTH = 400;
            } else {
                VIEWPORT_WIDTH = _tNode.clientWidth -1;
            }
            _container.style.width = VIEWPORT_WIDTH + "px";
        }
    }  
    
    this.loadURL = function(_url){
        if (typeof _url != 'undefined') {
            this.url = _url;
            var _i = new jmaki.Injector();
            _i.inject({url:_url, injectionPoint: _container});
        }
    }
    
    if (typeof widget.args != 'undefined') {
        if (typeof widget.args.topic != 'undefined') {
            topic = widget.args.topic;
        }
        if (typeof widget.args.url != 'undefined') {
            this.url = widget.args.url;
        }    
    }
    
    if (this.url != null) {
        this.loadURL(this.url);
    }
     var oldOnResizeEnd = null;
    
        if (window.onresize != null) {
            oldOnResizeEnd = window.onresize;
        }
    this.resize();
    window.onresize = function() {
        _this.resize();
        if (oldOnResizeEnd != null) oldOnResizeEnd();
    };   

        
    jmaki.subscribe(_topic, this.loadURL);
}

var dc = new jmaki.DContainer(widget);
jmaki.attributes.put(widget.uuid, dc);